package edu.scau.misp.backend_module.controller;

import edu.scau.misp.backend_module.core.domain.AjaxResult;
import edu.scau.misp.backend_module.domain.Dish;
import edu.scau.misp.backend_module.service.IDishService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping({"dish"})
public class DishController {
    @Autowired
    private IDishService dishService;


    @GetMapping("/sn/{dishSn}")
    public AjaxResult getDishBySn(@PathVariable("dishSn") Long dishSn) {
        return AjaxResult.success(dishService.getDishBySn(dishSn));
    }

    @GetMapping("/id/{dishId}")
    public AjaxResult getDishById(@PathVariable("dishId") Long dishId) {
        return AjaxResult.success(dishService.getDishById(dishId));
    }

    @GetMapping("/listAll")
    public AjaxResult listAllDish(){
        return AjaxResult.success(dishService.listAllDish());
    }

    @GetMapping("/list")
    public AjaxResult list(Dish dish) {
        List<Dish> list = dishService.selectDishList(dish);
        return AjaxResult.success(list);
    }

    @PostMapping
    public AjaxResult add(@RequestBody Dish dish) {
        int rows = dishService.insertDish(dish);
        return rows > 0 ? AjaxResult.success("添加餐品成功") : AjaxResult.error("添加餐品失败");
    }

    @PutMapping
    public AjaxResult edit(@RequestBody Dish dish) {
        int rows = dishService.updateDish(dish);
        return rows > 0 ? AjaxResult.success("修改餐品成功") : AjaxResult.error("修改餐品失败");
    }

    @DeleteMapping("/{dishSn}")
    public AjaxResult remove(@PathVariable("dishSn") Long dishSn) {
        int rows = dishService.deleteDishBySn(dishSn);
        return rows > 0 ? AjaxResult.success("删除餐品成功") : AjaxResult.error("删除餐品失败");
    }



}
